<template>
  <div>
    <section class="jumbotron">
        <h3 class="jumbotron-heading">Search Github Users</h3>
        <div>
            <input type="text" placeholder="enter the name you search" v-model="keyWord"/>&nbsp;
            <button @click="searchUsers">Search</button>
        </div>
    </section>
  </div>
</template>

<script>
import axios from 'axios'
import pubsub from 'pubsub-js'
    export default {
        name:'Serach',
        data(){
            return {
                keyWord:'',
            }
        },
        methods:{
            /* searchUsers(){
                // 请求前更新List的数据
                this.$bus.$emit('getItems',{isLoading:true,errMsg:'',users:[],isWelcome:false})
                axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
                    response=>{
                        console.log('请求成功');
                        // 请求成功后更新List的数据
                        this.$bus.$emit('getItems',{isLoading:false,errMsg:'',users:response.data.items})
                    },
                    error=>{
                        console.log('请求失败',error.data);
                        // 请求失败更新List的数据
                        this.$bus.$emit('getItems',{isLoading:false,errMsg:error.data,users:[]})
                    }
                )
            } */
            searchUsers(){ 
                // 发布消息
                pubsub.publish('getUsers',{isLoading:true,errMsg:'',users:[],isWelcome:false})
                this.$http.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
                    response=>{
                        console.log('请求成功'); 
                        // 请求成功后发布消息更新List的数据
                        pubsub.publish('getUsers',{isLoading:false,errMsg:'',users:response.data.items})
                    },
                    error=>{
                        console.log('请求失败'); 
                        // 请求失败发布消息更新List的数据
                        pubsub.publish('getUsers',{isLoading:false,errMsg:error.message,users:[]})
                    }
                )
            }
        }
    }
</script>

<style>

</style>